package com.htt.app.bienbaogiaothong.utils;

import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.htt.app.bienbaogiaothong.R;

public class Utils {
	private static String TAG = "Utils";
	
	public static void showDialog(final Context ctx, String title,
			String message) {

		String yes = ctx.getString(R.string.exit_app);

		AlertDialog.Builder dia = new AlertDialog.Builder(ctx);
		dia.setTitle(title);
		dia.setMessage(message);
		dia.setPositiveButton(yes, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Activity activity = (Activity) ctx;
				activity.finish();
				dialog.cancel();
			}
		});
		dia.create().show();
	}
	
	/**
	 * Kiểm tra tồn tại kết nối internet
	 * 
	 * @return
	 */
	public static boolean haveNetworkConnection(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		try {
			return cm.getActiveNetworkInfo().isConnectedOrConnecting();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/*
	 * return density Dpi device
	 */
	public static int getDensityDpi(Activity ctx) {
		DisplayMetrics dm = new DisplayMetrics();
		ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.densityDpi;
	}
	
	/*
	 * return width of screen device
	 */
	@SuppressWarnings("deprecation")
	public static int getWidthScreen(Activity ctx) {
		
		/***********************************************************/
    	WindowManager wm;
 		wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
 		Display d = wm.getDefaultDisplay();
 		
 		DisplayMetrics dm = new DisplayMetrics();
 		ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
 		
 		return d.getWidth();
 		/***************************************************/
	}
	
	/*
	 * return heigth of screen deivce
	 */
	@SuppressWarnings("deprecation")
	public static int getHeigthScreen(Activity ctx){
		/***********************************************************/
    	WindowManager wm;
 		wm = (WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE);
 		Display d = wm.getDefaultDisplay();
 		return d.getHeight();
 		/***************************************************/
	
	}
	
	
	/*
	 * get image from folder assets
	 */
	public static  Drawable getImageAssets(AssetManager assets, String name) {
		Drawable drawable = null;
		try {
			InputStream in = assets.open(name);
			drawable = Drawable.createFromStream(in, null);
			in.close();
		} catch (Exception e){
			Log.e(TAG ,"error image: " + name);
		}
		
		return drawable;
	}
}
